package com.example.controller;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.example.domain.Doctor;
import com.example.domain.LoginPojo;

import static com.example.utils.AjaxResult.*;

import com.example.domain.Patient;
import com.example.service.RootInfoService;
import com.example.utils.AjaxResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/root")
public class LoginController {

    @Autowired
    private RootInfoService rootInfoService;
    /** 验证码 */
    @RequestMapping("/getCode")
    public AjaxResult getCode(HttpServletRequest request) throws IOException {
        //生成随机数
        long rand = (long)(Math.random()*900000+100000);
        String code = String.valueOf(rand);
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI5tGaqh45FrGfSbdfgNgJ", "qzAmEQCZLtQ6YkLfQ0OX2gg9rV3E4Y");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setMethod(MethodType.POST);
        commonRequest.setDomain("dysmsapi.aliyuncs.com");
        commonRequest.setVersion("2017-05-25");
        commonRequest.setAction("SendSms");
        commonRequest.putQueryParameter("RegionId", "cn-hangzhou");
        commonRequest.putQueryParameter("PhoneNumbers", "18095055798");
        commonRequest.putQueryParameter("SignName", "张元浩的博客");
        commonRequest.putQueryParameter("TemplateCode", "SMS_461830486");
        Map<String, String> map = new HashMap<String, String>(2);
        map.put("code", code);
        commonRequest.putQueryParameter("TemplateParam", new ObjectMapper().writeValueAsString(map));
        try {
            CommonResponse commonResponse = client.getCommonResponse(commonRequest);
            System.out.println(commonResponse.getData());
        }catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println(code);
        request.getSession().setAttribute("captCode",code);
        return success(0,"发送成功");
    }
    /** 登录验证 */
    @RequestMapping("/loginIn")
    public AjaxResult loginIn(HttpServletRequest request, HttpSession session, String username,
                              LoginPojo loginPojo, Patient patient, Doctor doctor, Doctor admin){


        if (loginPojo.getCaptcha().equalsIgnoreCase(String.valueOf(request.getSession().getAttribute("captCode")))) {
            //患者登录
            if (0 == loginPojo.getPower()){

                patient.setPhone_number(loginPojo.getUsername());
                System.out.println(patient.getPhone_number());
                patient.setPwd(loginPojo.getPassword());
                Patient patient1 = new Patient();
                patient1 = rootInfoService.sltName(patient.getPhone_number());
                if (patient1 == null){
                    return AjaxResult.error("登录名不存在");
                }
                boolean login = rootInfoService.loginIn(patient);
                if (login) {
                    session.setAttribute("patient_id",patient1.getPatient_id());
                    session.setAttribute("usr",1);
                    session.setAttribute("patient_name",patient1.getPatient_name());
                    session.setAttribute("patient_sex",patient1.getSex());
                    session.setAttribute("phone_number",patient1.getPhone_number());
                    System.out.printf(patient1.toString());
                    return success(0,"登录成功");
                } else return error("登录名或密码错误");
            }

            //医生登录
            if (1 == loginPojo.getPower()){
                doctor.setDoctor_id(1);
                doctor.setDoctor_name("王五");
                doctor.setDoctor_department("口腔科");
                doctor.setPhone_number("111");
                doctor.setPwd("123456");
                session.setAttribute("id",doctor.getDoctor_id());
//                session.setAttribute("usr",1);
                session.setAttribute("username",doctor.getDoctor_name());
                session.setAttribute("patient",doctor);
                return success(1,"登录成功");
            }

            //管理登录
            if (2 == loginPojo.getPower()){
//                usrInfo.setUsrLogin(loginPojo.getUsername());
//                usrInfo.setUsrPwd(loginPojo.getPassword());
//                UsrInfo usr = usrInfoService.sltName(usrInfo);
//                if (usr == null){
//                    return error("登录名不存在");
//                }
//                login = usrInfoService.loginIn(usrInfo);
//                if (login) {
//                    //获取家属列表id
//                    System.out.println();
//                    System.out.println(usr.toString());
//                    System.out.println();
//                    Long older_list_id = usr.getOlderListId();
//                    List<HealthRecords> healthRecordsList = healthRecordsService.olders(older_list_id);
////                    HealthRecords older = healthRecordsService.older_dorm(olderName);
//                    HealthRecords older = healthRecordsList.get(0);
//                    HealthRecords older2 = new HealthRecords();
//                    if(healthRecordsList.size()>1){
//                        older2 = healthRecordsList.get(1);
//                    }
//                    usr.setOlderName1(older.getName());
//                    usr.setOlderName2(older2.getName());
//
//
//                    this.olderName = older.getName();
//                    session.setAttribute("username",usr.getUsrName());
//                    session.setAttribute("power",usr.getUsrPwd());
//                    session.setAttribute("usr",1);
//                    session.setAttribute("older",older);
//                    session.setAttribute("older2",older2);
//                    return success(2,"登录成功");
//                } else return error("登录名或密码错误");
                return success(2,"登录成功");
            }

        }
        return AjaxResult.error("验证码错误");

    }
}
